﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Text
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var sql = "select * from Students ";
            var dataTable = DbHelp.GetDataTable(sql);
            dataGridView1.DataSource = dataTable;

            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
        }
        //查询
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;

            var sqlStu =string.Format("select * from Students where Name like '{0}'", name);

            var dt = DbHelp.GetDataTable(sqlStu);
            dataGridView1.DataSource = dt;
        }
        //添加
        private void button2_Click(object sender, EventArgs e)
        {
            InitializeComponent();
            Form3 form3 = new Form3();
           var res= form3.ShowDialog();

            if (res == DialogResult.Yes)
            {
                var name = textBox1.Text;
                var sqlStu = string.Format("select * from Students");

                var dt = DbHelp.GetDataTable(sqlStu);

                dataGridView1.DataSource = dt;
                MessageBox.Show("yes");
            }
            else
            {
                MessageBox.Show("no");
            }
            
        }
        //删除
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["Id"].Value;
                var res = MessageBox.Show("确认删除吗", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (id > 0)
                {
                    if (res.Equals(DialogResult.Yes))
                    {
                        var sql = string.Format("delete from Students where Id={0}", id);
                        var rowCount = DbHelp.AddOrEditOrDeleteSave(sql);

                        if (rowCount > 0)
                        {
                            var name = textBox1.Text;
                            var sqlStu = string.Format("select * from Students");

                            var dt = DbHelp.GetDataTable(sqlStu);

                            dataGridView1.DataSource=dt;

                            MessageBox.Show("删除成功");
                            
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("没有选择任何的行");
            }
            

        }
        //更新
        private void button4_Click(object sender, EventArgs e)
        {
           

            if (dataGridView1.SelectedRows.Count > 0)
            {
                
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var name = (string)dataGridView1.SelectedRows[0].Cells["Name"].Value;
                var age = (int)dataGridView1.SelectedRows[0].Cells["Age"].Value;
                var score = (string)dataGridView1.SelectedRows[0].Cells["Scroe"].Value;
                Form3 form3 = new Form3(id, name, age,score);
               var res= form3.ShowDialog();
                if (res == DialogResult.Yes)
                {
                 var name1 = textBox1.Text;
                var sqlStu = string.Format("select * from Students");

                var dt = DbHelp.GetDataTable(sqlStu);

                dataGridView1.DataSource = dt;
                MessageBox.Show("yes");
                }
                
            }
            else
            {
                MessageBox.Show("未选择任何行");
            }
        }
    }
}
